package com.alongstudio.action.admin.settings;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.constants.SystemConstant;
import com.alongstudio.account.entity.DictionaryContent;
import com.alongstudio.account.entity.DictionaryType;
import com.alongstudio.account.service.IDictionaryService;

@SuppressWarnings("serial")
@Component("DictionaryAction")
@Scope("prototype")
public class DictionaryAction extends BaseAction {

	@Autowired
	@Qualifier("DictionaryService")
	private IDictionaryService dictService;
	private String className = DictionaryAction.class.getName();
	private DictionaryType dictType;
	private List<DictionaryType> dictTypeList;
	private DictionaryContent dictContent;
	private List<DictionaryContent> dictContentList;
	private String OPERATOR_RESULT;// 返回提示结果
	private String id;

	@Override
	public String executeAction() throws BusinessException {
		return SUCCESS;
	}

	/*********************************** 数据字典类型 *******************************************/
	/**
	 * 获取字典类型
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String getDictTypeData() throws BusinessException {
		try {
			Map result = dictService.findDictType(start, pageSize);
			dictTypeList = (List<DictionaryType>) result.get(SystemConstant.QUERY_RESULT_DATA);
			total = (Integer) result.get(SystemConstant.QUERY_RESULT_COUNT);
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	/**
	 * 新增or修改数据字典类型
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String newOrUpdateDictType() throws BusinessException {
		boolean isOk = false;
		try {
			if (dictType != null) {
				if (dictType.getId() != null) {
					isOk = dictService.updateDictType(dictType);
				}
				else {
					isOk = dictService.createDictType(dictType);
				}
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	/**
	 * 删除数据字典类型
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String deleteDictType() throws BusinessException {
		boolean isOk = false;
		try {
			if (id != null) {
				isOk = dictService.deleteDictType(id);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.DELETE_SUCCESS : IViewConstant.DELETE_FAILED;
		return SUCCESS;
	}

	/*********************************** 数据字典内容 *******************************************/

	/**
	 * 显示字典内容页面
	 */
	public String showDictContentPage() throws BusinessException {

		try {
			dictTypeList = dictService.findAllDictType();
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;

	}

	/**
	 * 得到字典内容数据
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String getDictContentData() throws BusinessException {
		try {
			if (id != null) {
				Map map = dictService.findDictCotent(id, start, pageSize);
				dictContentList = (List<DictionaryContent>) map.get(SystemConstant.QUERY_RESULT_DATA);
				total = (Integer) map.get(SystemConstant.QUERY_RESULT_COUNT);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	public String deleteDictContent() throws BusinessException {
		boolean isOk = false;
		try {
			if (id != null) {
				isOk = dictService.deleteDictContent(id);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.DELETE_SUCCESS : IViewConstant.DELETE_FAILED;
		return SUCCESS;

	}

	public String newOrUpdateDictContent() throws BusinessException {

		boolean isOk = false;
		try {
			if (dictContent != null) {
				if (dictContent.getId() != null) {
					isOk = dictService.updateDictContent(dictContent);
				}
				else {
					
					isOk = dictService.createDictContent(dictContent);
				}
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	
	}
	
	public DictionaryType getDictType() {
		return dictType;
	}

	public void setDictType(DictionaryType dictType) {
		this.dictType = dictType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<DictionaryType> getDictTypeList() {
		return dictTypeList;
	}

	public void setDictTypeList(List<DictionaryType> dictTypeList) {
		this.dictTypeList = dictTypeList;
	}

	public DictionaryContent getDictContent() {
		return dictContent;
	}

	public void setDictContent(DictionaryContent dictContent) {
		this.dictContent = dictContent;
	}

	public List<DictionaryContent> getDictContentList() {
		return dictContentList;
	}

	public void setDictContentList(List<DictionaryContent> dictContentList) {
		this.dictContentList = dictContentList;
	}

	public String getOPERATOR_RESULT() {
		return OPERATOR_RESULT;
	}

	public void setOPERATOR_RESULT(String oPERATOR_RESULT) {
		OPERATOR_RESULT = oPERATOR_RESULT;
	}

}
